package com.xq.chrome.web.other.conf;

import org.springframework.format.Formatter;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 调用spring框架对参数进行全局格式化
 *
 * @author MACHENIKE
 */
@Component
public class DateFormatter implements Formatter<Date> {
  private SimpleDateFormat dateFormat;

  @Override
  public Date parse(String text, Locale locale) throws ParseException {
    System.out.println("parse input text: " + text);
    System.out.println("parse date: " + dateFormat.parse(text));
    return dateFormat.parse(text);
  }

  public DateFormatter() {
    dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setLenient(false);
  }

  @Override
  public String print(Date object, Locale locale) {
    System.out.println("print method." + object);
    return dateFormat.format(object);
  }
}